package org.jbox2d.testbed.tests;

import org.jbox2d.collision.shapes.CircleDef;
import org.jbox2d.collision.shapes.PolygonDef;
import org.jbox2d.common.Vec2;
import org.jbox2d.dynamics.Body;
import org.jbox2d.dynamics.BodyDef;
import org.jbox2d.dynamics.World;
import org.jbox2d.testbed.AbstractExample;
import org.jbox2d.testbed.TestbedMain;

public class Test4 extends AbstractExample {
	
	public Test4(TestbedMain parent){
		super(parent);
	}
	
	private boolean firstTime = true;
	final float dwidth = .20f;
	final float dheight = 1.0f;
	float ddensity;// = 10f;
	final float dfriction = 0.1f;
	int baseCount = 25;
	public void makeDomino(float x, float y, boolean horizontal, World world) {
		PolygonDef sd = new PolygonDef();
		sd.setAsBox(.5f * dwidth, .5f * dheight);
		sd.density = ddensity;
		sd.friction = dfriction;
		sd.restitution = 0.65f;
		
		BodyDef bd = new BodyDef();
		bd.position = new Vec2(x, y);
		bd.angle = horizontal ? (float) (Math.PI / 2.0) : 0f;
		
		Body myBody = world.createBody(bd);
		myBody.createShape(sd);
		myBody.setMassFromShapes();
	}
	
	@Override
	public void create() {
		if (firstTime) {
			setCamera(0f, 12f, 10f);
			firstTime = false;
			settings.hz = 120;
		}
 
		{ // Floor
			PolygonDef sd = new PolygonDef();
			sd.setAsBox(50.0f, 10.0f);
 
			BodyDef bd = new BodyDef();
			bd.position = new Vec2(0.0f, -10.0f);
			m_world.createBody(bd).createShape(sd);
		}
 
		{
			CircleDef cd=new CircleDef();
			cd.radius=.1f;
			cd.density=2000000000;
			cd.restitution=0;
 
			BodyDef bodyDef=new BodyDef();
			bodyDef.position.set(-30.f,0.f);
			bodyDef.isBullet=true;
			Body body=m_world.createBody(bodyDef);
			body.setLinearVelocity(new Vec2(3000000000000000.f,0.f));
			body.createShape(cd);
			body.setMassFromShapes();
 
			body=m_world.createBody(bodyDef);
			body.setLinearVelocity(new Vec2(3000000000000000.f,500000000000000.f));
			body.createShape(cd);
			body.setMassFromShapes();
 
			body=m_world.createBody(bodyDef);
			body.setLinearVelocity(new Vec2(3000000000000000.f,1000000000000000.f));
			body.createShape(cd);
			body.setMassFromShapes();
 
			body=m_world.createBody(bodyDef);
			body.setLinearVelocity(new Vec2(3000000000000000.f,1500000000000000.f));
			body.createShape(cd);
			body.setMassFromShapes();
 
			body=m_world.createBody(bodyDef);
			body.setLinearVelocity(new Vec2(3000000000000000.f,2000000000000000.f));
			body.createShape(cd);
			body.setMassFromShapes();
 
			ddensity = 10f;
 
 
 
		}
 
		{
 
			// Make base
			for (int i = 0; i < baseCount; ++i) {
				float currX = i * 1.5f * dheight - (1.5f * dheight * baseCount / 2f);
				makeDomino(currX, dheight / 2.0f, false, m_world);
				makeDomino(currX, dheight + dwidth / 2.0f, true, m_world);
			}
			// Make 'I's
			for (int j = 1; j < baseCount; ++j) {
				if (j > 3)
					ddensity *= .8f;
				float currY = dheight * .5f + (dheight + 2f * dwidth) * .99f * j; // y
				// at
				// center
				// of
				// 'I'
				// structure
 
				for (int i = 0; i < baseCount - j; ++i) {
					float currX = i * 1.5f * dheight - (1.5f * dheight * (baseCount - j) / 2f);// +
					// random(-.05f,
					// .05f);
					ddensity *= 2.5f;
					if (i == 0) {
						makeDomino(currX - (1.25f * dheight) + .5f * dwidth, currY - dwidth, false, m_world);
					}
					if (i == baseCount - j - 1) {
						if (j != 1)
							makeDomino(currX + (1.25f * dheight) - .5f * dwidth, currY - dwidth, false, m_world);
					}
					ddensity /= 2.5f;
					makeDomino(currX, currY, false, m_world);
					makeDomino(currX, currY + .5f * (dwidth + dheight), true, m_world);
					makeDomino(currX, currY - .5f * (dwidth + dheight), true, m_world);
				}
			}
		}
	}
	
	@Override
	public String getName(){
		return "Test4";
	}
}
